package com.splant.android.smart.UILevel.Community.fragments;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.ModelLevel.PlantTypeListBean;
import com.splant.android.smart.UILevel.Community.Garden_PlantList_Activity;
import com.splant.android.smart.UILevel.Community.Garden_PlantList_Search_Activity;

import org.apache.http.Header;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class Garden_Ency_Fragment extends Fragment {

    private sPlantAsyncHttpClient sPlantClient;
    private View mView;
    private LRecyclerView lrv_plantTypeList;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private Garden_PlantTypeList_Adapter garden_plantTypeList_adapter;
    private List<PlantTypeListBean.PlantType> plantTypeList = new ArrayList<>();

    private EditText edt_searchPlant;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.garden_ency_fragment,null);
            edt_searchPlant= (EditText) mView.findViewById(R.id.edt_searchPlant);

            lrv_plantTypeList= (LRecyclerView) mView.findViewById(R.id.lrv_plantTypeList);

            sPlantClient = sPlantAsyncHttpClient.getInstance();

            init();

            edt_searchPlant.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(getActivity(), Garden_PlantList_Search_Activity.class);

                    startActivity(intent);
                }
            });

        }
        return mView;
    }

    private void init()
    {
        garden_plantTypeList_adapter = new Garden_PlantTypeList_Adapter(getActivity());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(garden_plantTypeList_adapter);

        lrv_plantTypeList.setHasFixedSize(true);
        lrv_plantTypeList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        lrv_plantTypeList.setAdapter(lRecyclerViewAdapter);
        lrv_plantTypeList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        lrv_plantTypeList.setFooterViewColor(R.color.colorAccent, R.color.garden_color2 ,android.R.color.white);
        lrv_plantTypeList.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊，点击再试一次吧");
        lrv_plantTypeList.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        lrv_plantTypeList.setOnRefreshListener(onRefreshListener);

        lrv_plantTypeList.setOnLoadMoreListener(onLoadMoreListener);

        garden_plantTypeList_adapter.setOnItemClickListener(new Garden_PlantTypeList_Adapter.onItemClickListener() {
            @Override
            public void onItemChick(int pos) {

                PlantTypeListBean.PlantType plantType= garden_plantTypeList_adapter.getDataList().get(pos);

                Intent intent=new Intent(getActivity(),Garden_PlantList_Activity.class);
                intent.putExtra("plantType",plantType);

                startActivity(intent);

            }
        });

        getDatas();
    }

    private static final int REQUEST_COUNT = 10000;
    public void getDatas() {

        RequestParams params = new RequestParams();
        lrv_plantTypeList.setNoMore(true);
        sPlantClient.get(sPlantAsyncHttpClient.GETPLANTTYPES, params, new sPlantJsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try
                {
                    if (response.getInt("succeed") == 1)
                    {
                        Gson gson = new Gson();

                        PlantTypeListBean plantTypeListBean = gson.fromJson(response.toString(), PlantTypeListBean.class);

                        if (plantTypeListBean.getPlantTypeList() != null&&plantTypeListBean.getPlantTypeList().size()>0) {
                            plantTypeList.clear();
                            plantTypeList.addAll(plantTypeListBean.getPlantTypeList());


                            garden_plantTypeList_adapter.setDataList(plantTypeList);

                            if (plantTypeListBean.getPlantTypeList().size()<REQUEST_COUNT)
                                lrv_plantTypeList.setNoMore(true);

                        }
                        else
                            lrv_plantTypeList.setNoMore(true);

                        lrv_plantTypeList.refreshComplete(REQUEST_COUNT);
                    }
                }
                catch (Exception e)
                {
                    lrv_plantTypeList.refreshComplete(REQUEST_COUNT);
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                lrv_plantTypeList.refreshComplete(REQUEST_COUNT);
                Toast.makeText(getActivity(), getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();
            }
        });

    }

    private OnRefreshListener onRefreshListener=new OnRefreshListener() {
        @Override
        public void onRefresh() {
            pageNo=0;
            plantTypeList.clear();
            getDatas();
        }
    };
    private int pageNo = 0;
    private OnLoadMoreListener onLoadMoreListener=new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            pageNo += 1;
            getDatas();
        }
    };

}
